﻿using System;
using System.Text;
using System.Web;
using log4net;

namespace FlickrXplorer.Web
{
    public class LogModule : IHttpModule
    {
        const string TAB = "\t";
       
        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            context.LogRequest +=new EventHandler(context_LogRequest);
        }

        void  context_LogRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            LogRequest(application.Context);
        }

        private static void LogRequest(HttpContext context)
        {
            ILog log = context.Items["Logger"] as ILog;

            StringBuilder builder = new StringBuilder();

            builder.Append(context.Request.HttpMethod);
            builder.Append(TAB);
            builder.Append(context.Request.Url.ToString());
            builder.Append(TAB);
            builder.Append(context.Request.UserAgent);
            builder.Append(";");
            builder.Append(context.Request.UserHostName);
            builder.Append(";");
            builder.Append(context.Response.StatusCode + "." + context.Response.SubStatusCode);

            log.Info(builder.ToString());
        }

    }
}
